function show()  //显示隐藏层和弹出层
{
   var hideobj=document.getElementsByClassName("hidebg")[0];
   console.log(hideobj);
   hideobj.style.display="block";  //显示隐藏层
   hideobj.style.height=document.body.clientHeight+"px";  //设置隐藏层的高度为当前页面高度
   // document.getElementsByTagName("iframe")[0].style.display="block";  //显示弹出层
   document.getElementsByClassName("mainLoginBox")[0].style.display = "block";
}
function hide()  //去除隐藏层和弹出层
{
   document.getElementsByClassName("hidebg")[0].style.display="none";
   document.getElementsByClassName("mainLoginBox")[0].style.display="none";
}
base_url = "http://glitfc.leanapp.cn";
function login(url){
   var username = document.getElementById("username").value;
   var password = document.getElementById("password").value;
   //步骤一:创建异步对象
   var ajax = new XMLHttpRequest();
   //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
   ajax.open('post', url);
   ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   //步骤三:发送请求

   //步骤四:注册事件 onreadystatechange 状态改变就会调用
   ajax.onreadystatechange = function () {
      if (ajax.readyState==4) {
         if (ajax.status==200) {
            console.log(ajax.responseText);//输入相应的内容
            var text = ajax.responseText;
            var json = JSON.parse(text);
            var code = json.code;
            if (code == 200){
               window.location.href = '/';
               // var req = new XMLHttpRequest();
            }else{
               alert(json.message);

            }
            }

         }
       //步骤五 如果能够进到这个判断 说明 数据 完美的回来了,并且请求的页面是存在的
   　　　　
     　　};
   ajax.send("username=" + username + "&password=" + password);
}

function register(url){
   var username = document.getElementById("username").value;
   var password = document.getElementById("password").value;
   var againPwd = document.getElementById("againPwd").value;
   var email = document.getElementById("email").value;
   if (username == null || username == "" || password == null || password == "" || againPwd == null || againPwd == ""){
      alert("请检查用户名/密码是否为空！");
   }else if (password == againPwd){
      //步骤一:创建异步对象
      var ajax = new XMLHttpRequest();
      //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
      ajax.open('post', url);
      ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      //步骤三:发送请求

      //步骤四:注册事件 onreadystatechange 状态改变就会调用
      ajax.onreadystatechange = function () {
         if (ajax.readyState==4) {
            if (ajax.status==200) {
               console.log(ajax.responseText);//输入相应的内容
               var text = ajax.responseText;
               var json = JSON.parse(text);
               var code = json.code;
               if (code == 200){
                  alert("注册成功");
                  login(base_url + '/user/login');
               } else{
                  alert(json.message);

               }

            }
          //步骤五 如果能够进到这个判断 说明 数据 完美的回来了,并且请求的页面是存在的
      　　　　
        　　}
      }
      ajax.send("username=" + username + "&password=" + password + "&email=" + email);
   } else {
      alert("两次输入密码不一致！请重新输入！");
   }

}
function find() {
   var findpwd = document.getElementsByClassName('main-div')[0];
   var loginBox = document.getElementsByClassName("mainLoginBox")[0];
   findpwd.setAttribute("style", "display: block");
   loginBox.setAttribute("style", "display: none");
}
function findpwd(url) {
   var email = document.getElementById("findpwd-text").value;
   console.log("email:" + email);
   if (email == null || email == ""){
      alert("请输入邮箱进行验证！");
   } else if(email.split('@')[1] == null || email.split('@')[1] == "" || email.split('.')[1] == null || email.split('.')[1] == ""){
      alert("请检查邮箱是否输入正确！");
   }else{
      var ajax = new XMLHttpRequest();
   ajax.open('post', url, true);
   ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   ajax.onreadystatechange = function () {
      if (ajax.readyState==4) {
         if (ajax.status==200) {
            console.log(ajax.responseText);
            var text = ajax.responseText;
            var json = JSON.parse(text);
            var code = json.code;
            if (code == 200){
               alert(json.message);
               window.location.href = '/';
            }else{
               alert(json.message);

            }
            }

         }

   　　　　
     　　};
   ajax.send("email=" + email);
   }


}
var userResp;
$(document).ready(function () {

    function get_user() {

         var login = document.getElementsByClassName("login")[0];
         var novel_login = document.getElementsByClassName("novel-login")[0];
         var fav = novel_login.getElementsByTagName("a")[0];
         var favourite = document.getElementsByClassName("fav")[0];
         var ajax = new XMLHttpRequest();
         //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
         ajax.open("get", base_url + "/user/get_user", true);
         ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         //步骤三:发送请求
         ajax.send(null);
         //步骤四:注册事件 onreadystatechange 状态改变就会调用
         ajax.onreadystatechange = function () {
            if (ajax.readyState==4) {
               if (ajax.status==200) {
                   var fav = novel_login.getElementsByTagName("a")[0];
                   var text = ajax.responseText;
                   var json = JSON.parse(text);
                   code = json.code;
                   if (code == 200){
                      Uname = json.data.username;
                      // user_id = json.data.objectId;
                      if (Uname != null){
                           login.innerText = Uname + "/退出登录";
                           login.setAttribute("href", "/");
                           fav.setAttribute("href", "book-fav.html");

                       }
                      // return user_id;
                   }else {
                      Uname = null;
                      fav.setAttribute("href", "login.html");
                   }
               }
            }
         };
         login.onclick = function () {
                     if (login.innerText == "登录/注册") {

                     }else{
                        var resp = httpReq(base_url + "/user/logout","get");

                        console.log("resp:" + resp);

                     }
                 }
         function httpReq(url, method, param) {
               //步骤一:创建异步对象
            var ajax = new XMLHttpRequest();
            //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
            ajax.open(method, url, true);
            ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            //步骤三:发送请求
            if(method == "post"){
                console.log(param);
                for(var i = 0; i<param.length; i++){
                    var key = param[i];
                }
                ajax.send();
                //步骤四:注册事件 onreadystatechange 状态改变就会调用
                ajax.onreadystatechange = function () {
                   if (ajax.readyState==4) {
                      if (ajax.status==200) {
                          console.log("ajax.responseText:" + ajax.responseText);
                          return ajax.responseText;
                      }
                   }
                }
            }else{
                ajax.send(null);
                //步骤四:注册事件 onreadystatechange 状态改变就会调用
                ajax.onreadystatechange = function () {
                   if (ajax.readyState==4) {
                      if (ajax.status==200) {
                          console.log("ajax.responseText:" + ajax.responseText);
                          return ajax.responseText;
                      }
                   }
                }
            }

            console.log(ajax.readyState);


          }
          console.log(ajax);
          return ajax;

    }
    get_user();
});
